package com.sy.auth.controller;

import com.baomidou.mybatisplus.core.conditions.query.LambdaQueryWrapper;
import com.baomidou.mybatisplus.core.toolkit.StringUtils;
import com.baomidou.mybatisplus.core.toolkit.support.SFunction;
import com.baomidou.mybatisplus.extension.plugins.pagination.Page;
import com.sy.auth.service.SysUserService;
import com.sy.common.result.Result;
import com.sy.common.utils.MD5;
import com.sy.model.system.SysUser;
import com.sy.vo.system.SysUserQueryVo;
import io.swagger.annotations.Api;
import io.swagger.annotations.ApiOperation;
import org.springframework.beans.factory.annotation.Autowired;
import org.springframework.security.access.prepost.PreAuthorize;
import org.springframework.web.bind.annotation.*;

import java.util.List;
import java.util.Map;

/**
 * <p>
 * 用户表 前端控制器
 * </p>
 *
 * @author sy
 * @since 2023-03-31
 */
@Api(tags = "用户管理")
@RestController
@RequestMapping("admin/system/sysUser")
@CrossOrigin
public class SysUserController {

    @Autowired
    SysUserService sysUserService;

    @ApiOperation(value = "获得当前用户信息")
    @GetMapping("/getCurrentUser")
    public Result getCurrentUser(){
        Map<String, Object> map = sysUserService.getCurrentUser();
        return Result.ok(map);
    }

    // 分页查询
    @PreAuthorize("hasAuthority('bnt.sysUser.list')")
    @ApiOperation(value = "分页查询")
    @GetMapping("{page}/{limit}")
    public Result index(@PathVariable Long page, @PathVariable Long limit,
                        SysUserQueryVo sysUserQueryVo){

        Page<SysUser> sysUserPage = new Page<SysUser>(page, limit);

        LambdaQueryWrapper<SysUser> wrapper = new LambdaQueryWrapper<>();

        String username = sysUserQueryVo.getKeyword();
        String createTimeBegin = sysUserQueryVo.getCreateTimeBegin();
        String createTimeEnd = sysUserQueryVo.getCreateTimeEnd();

        // 设置查询条件
        if(!StringUtils.isEmpty(username)){
            wrapper.like(SysUser::getName, username);
        }
        if(!StringUtils.isEmpty(createTimeBegin)){
            wrapper.ge(SysUser::getCreateTime, createTimeBegin);
        }
        if(!StringUtils.isEmpty(createTimeEnd)){
            wrapper.le(SysUser::getCreateTime, createTimeEnd);
        }

        Page<SysUser> userPage = sysUserService.page(sysUserPage, wrapper);
        return Result.ok(userPage);
    }

    // 获取用户
    @PreAuthorize("hasAuthority('bnt.sysUser.list')")
    @ApiOperation(value = "获得用户")
    @GetMapping("get/{id}")
    public Result get(@PathVariable Long id){
        return Result.ok(sysUserService.getById(id));
    }

    // 保存用户
    @PreAuthorize("hasAuthority('bnt.sysUser.add')")
    @ApiOperation(value = "保存用户")
    @PostMapping ("save")
    public Result save(@RequestBody SysUser sysUser){
        //对密码进行加密，使用MD5
        String encrypt = MD5.encrypt(sysUser.getPassword());
        sysUser.setPassword(encrypt);

        boolean isSuccess = sysUserService.save(sysUser);
        return isSuccess ? Result.ok() : Result.fail();
    }

    // 更新用户
    @PreAuthorize("hasAuthority('bnt.sysUser.update')")
    @ApiOperation(value = "更新用户")
    @PutMapping ("update")
    public Result update(@RequestBody SysUser sysUser){
        boolean isSuccess = sysUserService.updateById(sysUser);
        return isSuccess ? Result.ok() : Result.fail();
    }

    // 删除用户
    @PreAuthorize("hasAuthority('bnt.sysUser.remove')")
    @ApiOperation(value = "删除用户")
    @DeleteMapping ("remove/{id}")
    public Result remove(@PathVariable Long id){
        boolean isSuccess = sysUserService.removeById(id);
        return isSuccess ? Result.ok() : Result.fail();
    }

    @PreAuthorize("hasAuthority('bnt.sysUser.update')")
    @ApiOperation(value = "更新状态")
    @GetMapping("updateStatus/{id}/{status}")
    public Result updateStatus(@PathVariable Long id, @PathVariable Integer status) {
        sysUserService.updateStatus(id, status);
        return Result.ok();
    }

}
